package com.winter.iterator;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 * @program: Collection
 * @description: LinkedList集合详解
 * @author: Mr.Liang
 * @create: 2018-01-08 17:56
 **/
public class LinkedListIterator {
    /*容量*/
    private static final int SIZE = 100000;

    /*测试遍历LinkedList几种方式的效率*/
    public static void main(String[] args) {
//        通过迭代器遍历集合
        linkedListIterator(getLinkedList());
//        快速随机遍历
        linkedListFor(getLinkedList());
//        foreach遍历
        linkedListForeach(getLinkedList());
//        pollFirst()遍历
        linkedListPollFirst(getLinkedList());
//        pollLast()遍历
        linkedListPollLast(getLinkedList());
//        removeFirst()遍历
        linkedListRemoveFirst(getLinkedList());
//        removeLast()遍历
        linkedListRemoveLast(getLinkedList());
    }


    /*新建一个LinkedList对象，添加元素到集合中*/
    private static LinkedList getLinkedList() {
        LinkedList list = new LinkedList<>();
        for (int i = 0; i < SIZE; i++) {
            list.addLast(i);
        }
        return list;
    }

    /*iterator遍历集合*/
    private static void linkedListIterator(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            iterator.next();
        }
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("iterator遍历：" + value + "ms");
    }

    /*快速随机遍历*/
    private static void linkedListFor(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            list.get(i);
        }
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("快速随机遍历：" + value + "ms");
    }

    /*foreach遍历*/
    private static void linkedListForeach(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        for (Object one : list
                )
            ;
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("foreach遍历：" + value + "ms");
    }

    /* pollFirst()遍历*/
    private static void linkedListPollFirst(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        while (list.pollFirst() != null) ;
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("pollFirst()遍历：" + value + "ms");
    }

    /*pollLast()遍历*/
    private static void linkedListPollLast(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        while (list.pollLast() != null) ;
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("pollLast()遍历：" + value + "ms");
    }

    /*removeFirst()遍历*/
    private static void linkedListRemoveFirst(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        try {
            while (list.removeFirst() != null) ;
        } catch (NoSuchElementException e) {
        }
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("removeFirst()遍历：" + value + "ms");
    }

    /*removeLast()遍历*/
    private static void linkedListRemoveLast(LinkedList list) {
        if (list == null)
            return;
//        记录开始时间
        long start = System.currentTimeMillis();
        try {
            while (list.removeLast() != null) ;
        } catch (NoSuchElementException e) {
        }
//        记录结束时间
        long end = System.currentTimeMillis();
        long value = end - start;
        System.out.println("removeLast()遍历：" + value + "ms");
    }

}
